package avl.sv.client.tools;

import avl.sv.client.MouseActionLogger;
import avl.sv.client.SlideViewer;
import avl.sv.shared.Annotations.ROI;
import avl.sv.shared.Annotations.ROIOval;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;

/**
 *
 * @author benbryan
 */
public class OvalTool extends AbstractSlideViewerTool {
    private ROI workingROI = null;
    private MouseActionLogger mouseAction = new MouseActionLogger();

    final SlideViewer slideViewer;

    public OvalTool(SlideViewer slideViewer) {
        this.slideViewer = slideViewer;
        setToolTipText("Oval Tool");
        setIcon(new javax.swing.ImageIcon(getClass().getResource("/avl/sv/client/icon/oval.png")));

    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        if (e.getButton() != MouseEvent.BUTTON1) {
            return;
        }
        mouseAction.mouseClicked(e);
        slideViewer.repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e)   {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mouseReleased(e);
        if (mouseAction.getMouseButton1() == MouseActionLogger.ButtonStatus.RELEASED) {
            slideViewer.getRoiManager().addROI(workingROI);
            workingROI = null;
            slideViewer.getToolPanel().setDefaultTool();
        }
        slideViewer.repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mouseDragged(e);

        if (mouseAction.getMouseButton1() == MouseActionLogger.ButtonStatus.PRESSED) {
            Point pImg = slideViewer.displaySpaceToImageSpace(e.getPoint());
            addingNewRoi(pImg);
        }
        slideViewer.repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mousePressed(e);

        if (mouseAction.getMouseButton1() == MouseActionLogger.ButtonStatus.PRESSED) {
            Point pImg = slideViewer.displaySpaceToImageSpace(e.getPoint());
            addingNewRoi(pImg);
        }
        slideViewer.repaint();
    }

    private void addingNewRoi(Point pImg) {

        slideViewer.getRoiManager().setRoisModified(true);
        int ix = (int) pImg.getX();
        int iy = (int) pImg.getY();

        if (workingROI == null) {
            workingROI = new ROIOval(ix, iy);
        } else {
            ((ROIOval) workingROI).addSecondPoint(ix, iy);
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    
    @Override
    public void paintOnSlideViewer(Graphics2D g) {
        if (workingROI != null){
            slideViewer.concatenateImageToDisplayTransform(g);
            workingROI.paintROI(g, Color.GREEN);
        }
    }
    
}
